home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / aijournl / 1988_08 / kohplot.c < prev    next >
C/C++ Source or Header  |  1988-07-22  |  9KB  |  265 lines

  1. /*********************************************************
  2.   Kohonen Network Plotting Program
  3.   Written by Maureen Caudill 
  4.   in Lightspeed C on a Macintosh
  5.   
  6.  See the August, 1988 AI Expert, Neural Network Primer, Part 4 
  7.   for a discussion of this network.
  8.   
  9.   This program takes the output from Kohonen Feature Map.c and
  10.   generates feature maps on a Macintosh screen.
  11.   
  12.   Note, you MUST run Kohonen.c prior to running this program.
  13.   The data files required for this program are generated by
  14.   running the feature map program; only after they are generated
  15.   can the topological maps be drawn.
  16.   
  17.   The program looks for the specified data files, and, one at
  18.   at time, draws the feature map for that file.  To go to the
  19.   next file, press RETURN.  The feature map program generates 
  20.   20 data files, every 100 input patterns.  This simple program
  21.   does not label the screen.  Note that the axes are precisely
  22.   one unit long in each direction.
  23.   
  24.   ****MACINTOSH SPECIFIC PROGRAM;  NOT USABLE ELSEWHERE!!***
  25.   
  26.   ****ALSO, NOTE THAT THE DISPLAY IN THIS PROGRAM HAS BEEN ****
  27.   ****SCALED FOR OPERATION ON A MacII 13 INCH MONITOR.  ON ****
  28.   ****A REGULAR MACINTOSH, YOU MUST CHANGE THE SCALING     ****
  29.   ****PARAMETERS AS INDICATED BELOW TO SEE THE ENTIRE SCREEN ****
  30.   
  31.   ⌐Adaptics
  32.   16776 Bernardo Center Drive, Suite 110B
  33.   San Diego, CA  92128
  34.   (619) 451-3752
  35.  
  36.   *********************************************************/
  37. #include   <math.h>            /*needed for floating point and math functions*/
  38. #include   <stdio.h>        /* gives fopen, fclose, printf functions */
  39.  #include "WindowMgr.h"
  40.  #include "EventMgr.h"
  41.  #include "QuickDraw.h"
  42.  
  43. #define        NUMROWS     10             /* number of neurodes = NUMROWS*NUMCOLS */
  44. #define       NUMCOLS     10
  45. #define        PATSIZE     2             /* keep input size small for easy plotting */
  46. #define        NUMITERS      500             /* total iterations to run */
  47. #define        SAVECOUNT      50          /* when to save net (about 0.1*NUMITERS) */
  48. #define        ADJNBORS      100          /* how often to lower neighborhood size */ 
  49.  
  50. /**** change the following parameters if not using a 13 inch Mac screen ****/
  51. #define        WINDHEIGHT  500         /* if regular MAC, this should be about 300 */
  52. #define     WINDWIDTH    500         /* if regular MAC, this should be about 300 */
  53. #define        BORDER        50            /* this puts a 50 pixel border within window, allowing
  54.                                         room for menu bar at top and a margin all around */
  55. /*-----------------------------------------------------------------------------------------------------------
  56.    Global storage
  57. -------------------------------------------------------------------------------------------------------------*/
  58. int        intwts[NUMROWS][NUMCOLS][PATSIZE]            ; /* integer-converted weights */
  59. int        count;
  60. FILE    *fopen(),*savefile                            ; /* output file to save network state */
  61. char    *strcpy();
  62. char    *strcat();
  63. char  *savepath,
  64.       *savepath0 = "Kohonen Iter 0",
  65.       *savepath1 = "Kohonen Iter 100",
  66.       *savepath2 = "Kohonen Iter 200",
  67.       *savepath3 = "Kohonen Iter 300",
  68.       *savepath4 = "Kohonen Iter 400",
  69.       *savepath5 = "Kohonen Iter 500",
  70.       *savepath6 = "Kohonen Iter 600",
  71.       *savepath7 = "Kohonen Iter 700",
  72.       *savepath8 = "Kohonen Iter 800",
  73.       *savepath9 = "Kohonen Iter 900",
  74.       *savepath10 = "Kohonen Iter 1000",
  75.       *savepath11 = "Kohonen Iter 1100",
  76.       *savepath12 = "Kohonen Iter 1200",
  77.       *savepath13 = "Kohonen Iter 1300",
  78.       *savepath14 = "Kohonen Iter 1400",
  79.       *savepath15 = "Kohonen Iter 1500",
  80.       *savepath16 = "Kohonen Iter 1600",
  81.       *savepath17 = "Kohonen Iter 1700",
  82.       *savepath18 = "Kohonen Iter 1800",
  83.       *savepath19 = "Kohonen Iter 1900",
  84.       *savepath20 = "Kohonen Iter 2000";
  85.     
  86.       
  87.         
  88. /*--------------------------------------------------------------------------------------------------
  89.  
  90.     READ_DATA(iteration)
  91.     This routine will read the current state of the weight 
  92.     vectors from a file so they can later be plotted.
  93.     The format of the data file is:
  94.     
  95.     Iteration number
  96.     neurode 0,0 weights 
  97.     neurode 0,1 weights
  98.     neurode 0,2 weights
  99.     ...
  100.     neurode 1,0 weights
  101.     ...
  102.     neurode 9,9 weights
  103.     Iteration number
  104.     ...
  105.     The routine or program reading this file must be told the
  106.     number of neurodes in each row and column of the network,
  107.     and the number of elements in the input pattern.
  108.     
  109. -----------------------------------------------------------------------------------------------------*/
  110. read_data()
  111. {
  112.     int        row, col, wt;
  113.     char        *strcpy();
  114.     char        *strcat();
  115.     char        digits;
  116.     char        itoa();
  117.     float        weight1,weight2,xscale,yscale,xoffset,yoffset;
  118.     int            readcount;
  119.     
  120.     savefile = fopen(savepath,"r");         /* open an input text file */
  121.     if (savefile == NULL)
  122.     {
  123.         printf("\n Error in opening file");
  124.         return;
  125.     }
  126.     xscale = (float) (WINDWIDTH/2 - BORDER);        /* scaling factor for width */
  127.     yscale = (float) (WINDHEIGHT/2 - BORDER);       /* scaling factor for height */
  128.     xoffset = (float) (WINDWIDTH/2);                /* offset to X origin */
  129.     yoffset = (float) (WINDHEIGHT/2);               /* offset to Y origin */
  130.     readcount = fscanf(savefile,"\n Iteration count %d",&count);   /* iteration count */
  131.  
  132.     for (row=0; row<NUMROWS; row++)
  133.     {
  134.         for (col=0; col<NUMCOLS; col++)
  135.         {
  136.                 fscanf(savefile,"%f %f",&weight1, &weight2);
  137.                 weight2 = -weight2;  /* flip because vertical grows down on screen */
  138.                 intwts[row][col][0] = (int)(xscale * weight1 + xoffset);
  139.                 intwts[row][col][1] = (int)(yscale * weight2 + yoffset);
  140.         }  /* end for each neurode in column */
  141.     }  /* end for each neurode in row */
  142.     fclose(savefile);
  143.     return;
  144. }
  145.  
  146.  /*------------------------------------------------------------------
  147.  
  148.      Main program
  149.      
  150.  --------------------------------------------------------------------*/
  151. main()
  152. {
  153.      WindowPtr myWindow, whichWindow;
  154.      EventRecord    myEvent;
  155.      static Rect bounds = {0,0,WINDWIDTH,WINDHEIGHT};
  156.      int            row, col, pat,done,xleft,xright,ytop,ybottom,xcenter,ycenter;
  157.      int            scale, figureLeft, figureTop,length;
  158.      int            filecount,code,count;
  159.     
  160.     for (filecount=0; filecount<21; filecount++)
  161.     {
  162.         switch (filecount)
  163.         {
  164.         case 0:  savepath= savepath0; break;
  165.         case 1:  savepath= savepath1; break;
  166.         case 2:  savepath= savepath2; break;
  167.         case 3:  savepath= savepath3; break;
  168.         case 4:  savepath= savepath4; break;
  169.         case 5:  savepath= savepath5; break;
  170.         case 6:  savepath= savepath6; break;
  171.         case 7:  savepath= savepath7; break;
  172.         case 8:  savepath =savepath8; break;
  173.         case 9:  savepath= savepath9; break;
  174.         case 10:  savepath = savepath10; break;
  175.         case 11:  savepath= savepath11; break;
  176.         case 12:  savepath= savepath12; break;
  177.         case 13:  savepath= savepath13; break;
  178.         case 14:  savepath= savepath14; break;
  179.         case 15:  savepath= savepath15; break;
  180.         case 16:  savepath= savepath16; break;
  181.         case 17:  savepath= savepath17; break;
  182.         case 18:  savepath =savepath18; break;
  183.         case 19:  savepath= savepath19; break;
  184.         case 20:  savepath = savepath20; break;
  185.     
  186.         default:    savepath=savepath20;break;
  187.         }
  188.  
  189.         read_data();
  190.     
  191.          InitGraf(&thePort);
  192.         InitFonts();
  193.          InitWindows();
  194.          InitCursor();
  195.          TextFont(3);         /* In most systems this should be Geneva font */
  196.  
  197.           xleft = BORDER;
  198.          ytop  = BORDER;
  199.          xcenter = WINDWIDTH/2;
  200.          ycenter = WINDHEIGHT/2;
  201.          xright = WINDWIDTH-BORDER;
  202.          ybottom = WINDHEIGHT-BORDER;
  203.     
  204.          myWindow = NewWindow(0,&bounds, "\pClick close box or Return",1,0,-1,1,0);
  205.     
  206.         /*SetOrigin (-xcenter, -ycenter);*/   /*offset origin to window location */
  207.  
  208.          /* draw X-Y axes */
  209.          MoveTo(xleft,ycenter);  /* Starting 50 pixels from top leaves room for menu bar */
  210.          LineTo(xright,ycenter);
  211.          MoveTo(xcenter,ytop);
  212.          LineTo(xcenter,ybottom);
  213.  
  214.         /* draw the topology map */
  215.         for (row = 0; row<NUMROWS; row++)
  216.         {    
  217.             MoveTo(intwts[row][0][0], intwts[row][0][1]);
  218.             for (col=1; col<NUMCOLS; col++)
  219.             {
  220.                 LineTo (intwts[row][col][0],intwts[row][col][1]);
  221.             }
  222.         }
  223.     
  224.         for (col = 0; col<NUMCOLS; col++)
  225.         {
  226.             MoveTo(intwts[0][col][0], intwts[0][col][1]);
  227.             for (row=1; row<NUMROWS; row++)
  228.             {
  229.                 LineTo (intwts[row][col][0], intwts[row][col][1]);
  230.             }
  231.         }
  232.         
  233.         MoveTo(xcenter-10,ybottom+20);   /* almost centered, just below diagram */
  234.         count = filecount;
  235.         if (count>=10)
  236.         {
  237.             code = count/10 + 48;       /* make code = ascii code for digit */
  238.             DrawChar(code);
  239.             count -= 10*(code-48);
  240.         }
  241.         code = count + 48;              /* make code = ascii code for digit */
  242.         DrawChar(code);
  243.         DrawChar('0');
  244.         DrawChar('0');
  245.         done = FALSE;
  246.          while (done!=TRUE ) 
  247.          {
  248.              GetNextEvent(everyEvent, &myEvent);
  249.              if ( (myEvent.what == mouseDown  
  250.                      && FindWindow(myEvent.where, &whichWindow) == inGoAway
  251.                      && whichWindow == myWindow
  252.                      && TrackGoAway (myWindow, myEvent.where) )
  253.                  || myEvent.what == keyDown)
  254.                  {
  255.                      HideWindow(myWindow);
  256.                      DisposeWindow(myWindow);
  257.                      done = TRUE;    
  258.                  }
  259.              else
  260.                  done = FALSE;
  261.          }
  262.          
  263.      }
  264.      return;
  265.  }